/*
Source : https://leetcode.com/problems/zigzag-conversion/
Author : nflush@outlook.com
Date   : 2016-05-03
*/

/*
6. ZigZag Conversion  
Total Accepted: 86686 Total Submissions: 361039 Difficulty: Easy

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

Subscribe to see which companies asked this question
*/


class Solution {
public:
    string convert(string s, int numRows) {
        
    }
};

char* convert(char* s, int numRows) {
    int len = strlen(s);
    char *ret = malloc(len + 1);
    ret[len] = '\0';
    int i,j,pos = 0;
    if (numRows == 1){
        strcpy(ret, s);
        return ret;
    }
    ret[0] = s[0];
    int x = numRows + numRows -2;
    int y = 0;
    for (i = 0; i < numRows; i++){
        ret[pos++] = s[i];
        for (j = i; j < len;){
            j+=x;
            if (j < len && x != 0)
                ret[pos++] = s[j];
            j+=y;
            if (j < len && y != 0)
                ret[pos++] = s[j];
        }
        x-=2;
        y+=2;
    }
    return ret;
}

